package com.zr.repertory.areLocation.controller;

import com.zr.areLocation.model.*;
import com.zr.areLocation.service.AreLocationService;
import com.zr.enumvo.StatusEnum;
import com.zr.util.ResultBuilderVo;
import com.zr.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 77509 on 2019/11/20.
 */
@RestController
@CrossOrigin
public class AreLocationController {
    @Autowired
    private AreLocationService areLocationService;
    @PostMapping("areLocation/add")
    public ResultVo add(@RequestBody @Valid AreLocationAddVo areLocationAddVo, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return areLocationService.add(areLocationAddVo);
    }
    @PostMapping("areLocation/update")
    public ResultVo update(@RequestBody @Valid AreLocationUpdateVo areLocationUpdateVo, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return areLocationService.update(areLocationUpdateVo);
    }

    @GetMapping("areLocation/see")
    public ResultVo see(Integer id){

        if (id == null){
            return ResultBuilderVo.error("id不能为空");
        }
        return areLocationService.see(id);
    }

    @PostMapping("areLocation/queryPage")
    public ResultVo queryPage(@RequestBody AreLocationSelectVo areLocationSelectVo){

        return areLocationService.queryPage(areLocationSelectVo);
    }

    @PostMapping("areLocation/updateStatus")
    public ResultVo updateStatus(@RequestBody @Valid AreLocationUpdateStatusVo areLocationUpdateStatusVo, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return areLocationService.updateStatus(areLocationUpdateStatusVo);
    }

    @PostMapping("areLocation/getPullDownList")
    public ResultVo getPullDownList(){

        Map<String,List<StatusVo>> xialamap = new HashMap<>();
        List<StatusVo> statusVoList = new ArrayList<>();
        for (StatusEnum c : StatusEnum.values()) {
            StatusVo statusVo = new StatusVo(c.getIndex(),c.getName());
            statusVoList.add(statusVo);

        }
        xialamap.put("status",statusVoList);
        return ResultBuilderVo.success(xialamap);
    }
}
